#!/usr/bin/env python3
"""
检查星露谷物语Steam版注册表路径的测试脚本
"""

import winreg
from typing import Optional, Tuple


def check_registry_path(hkey, path: str) -> Optional[Tuple[str, dict]]:
    """检查注册表路径并返回找到的信息"""
    try:
        key = winreg.OpenKey(hkey, path)
        values = {}
        
        # 尝试读取所有常见的值
        value_names = [
            "InstallLocation",
            "DisplayName",
            "DisplayVersion",
            "Publisher",
            "InstallDate",
            "UninstallString",
            "DisplayIcon",
        ]
        
        for value_name in value_names:
            try:
                value, reg_type = winreg.QueryValueEx(key, value_name)
                values[value_name] = value
            except FileNotFoundError:
                continue
        
        winreg.CloseKey(key)
        return (path, values)
    except (OSError, FileNotFoundError) as e:
        return None


def find_stardew_path_from_registry() -> Optional[str]:
    """从Windows注册表查找星露谷物语Steam版安装路径（与translate_mods.py相同逻辑）"""
    registry_paths = [
        # 64位系统
        (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150"),
        (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150"),
        # 32位系统或用户安装
        (winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150"),
    ]
    
    # 尝试查找 InstallLocation
    for hkey, path in registry_paths:
        try:
            key = winreg.OpenKey(hkey, path)
            try:
                install_location = winreg.QueryValueEx(key, "InstallLocation")[0]
                if install_location:
                    install_location = install_location.strip('"').strip()
                    return install_location
            finally:
                winreg.CloseKey(key)
        except (OSError, FileNotFoundError):
            continue
    
    return None


def main():
    print("=" * 70)
    print("星露谷物语Steam版注册表检查工具")
    print("=" * 70)
    print()
    
    # 要检查的注册表路径
    registry_paths = [
        (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150", "HKEY_LOCAL_MACHINE"),
        (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150", "HKEY_LOCAL_MACHINE (WOW64)"),
        (winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150", "HKEY_CURRENT_USER"),
    ]
    
    print("检查注册表路径：")
    print("-" * 70)
    for hkey, path, desc in registry_paths:
        print(f"\n[{desc}]")
        print(f"路径: {path}")
        
        result = check_registry_path(hkey, path)
        if result:
            reg_path, values = result
            print(f"  [OK] 找到注册表项")
            
            # 显示所有找到的值
            for key, value in values.items():
                print(f"    {key}: {value}")
            print()
        else:
            print(f"  [FAIL] 未找到注册表项")
    
    print("=" * 70)
    print("\n使用translate_mods.py的逻辑查找结果：")
    print("-" * 70)
    
    found_path = find_stardew_path_from_registry()
    if found_path:
        print(f"[OK] 找到安装路径: {found_path}")
        print(f"\nMods目录路径: {found_path}\\Mods")
    else:
        print("[FAIL] 未找到安装路径")
    
    print("\n" + "=" * 70)
    print("检查完成！")
    print("=" * 70)


if __name__ == "__main__":
    main()

